<?php
/* SVN FILE: $Id: item_galleryavatar.php 247 2009-10-22 15:31:39Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Upload a Gallery Avatar item
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 247 $
* @lastrevision $Date: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/items/own/item_galleryavatar.php $
*/

class item_galleryavatar extends templateItem
{
	/**
	 * The default name of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultName = "Upload a Gallery Avatar";
	
	/**
	 * The default description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultDescription = "Upload a gallery avatar for people to use for their avatar.";
	
	// Inherited Properties
	/**
	 * Item setting to allow multiple instances within ibMarket 
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $allowMultiple = true;
	
	/**
	 * The location of the gallery directory
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $galleryDirectory;
	
	/**
	 * Constructor to setup the item
	 *
	 * @access	public
	 * @param	ipsRegistry	$registry	IPB registry
	 * @param	array		&$item		The item information
	 */
	public function __construct(ipsRegistry $registry, array &$item)
	{
		parent::__construct($registry, $item);
		
		$this->galleryDirectory = DOC_IPS_ROOT_PATH . PUBLIC_DIRECTORY . '/style_avatars/IBMarket_Avatars';
	}
	
	/**
	 * Additional settings when adding/editing the item
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminForm()
	{
		return;
	}
	
	/**
	 * Saving to adding/editing the item
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	array	[$input]		Relavent input relating to the item
	 * @return	void
	 */
	public function adminSave(array &$item_info, array $input)
	{
		if( is_dir($this->galleryDirectory) )
		{
			if( !is_writeable($this->galleryDirectory) && chmod($this->galleryDirectory, 0777) )
			{
				$this->registry->output->global_message .= $this->lang->words['gallery_avatar_made_writable'];
			}
			else
			{
				$this->registry->output->global_message .= $this->lang->words['gallery_avatar_folder_ready'];
			}
		}
		else
		{
			if( is_writeable(DOC_IPS_ROOT_PATH . PUBLIC_DIRECTORY . '/style_avatars') && mkdir($this->galleryDirectory, 0777) )
			{
				$this->registry->output->global_message .= $this->lang->words['gallery_avatar_folder_made'];
			}
			else
			{
				$this->registry->output->global_message .= $this->lang->words['gallery_avatar_folder_made'];
			}
		}
	}
	
	/**
	 * When quick saving the price and stock
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	string	$type			Possible values: 'stockprice', more possibly in the future
	 * @return	void
	 */
	public function adminQuickSave(array &$item_info, $type)
	{
		return;
	}
	
	/**
	 * When removing the item from ibMarket
	 *
	 * @access	public
	 * @return	void
	 */
	public function adminRemove()
	{
		return;
	}
	
	/**
	 * Viewing the file edits if any
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminViewFileEdits()
	{
		return false;
	}
	
	/**
	 * Any admin AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function adminAjax()
	{
		return;
	}
	
	/**
	 * Check and modify the intentory item information before storing in the database
	 *
	 * @access	public
	 * @param	array	&$invItem		The new inventory item information to be stored in the database.
	 * @return	bool	true if the item is successfully created, false otherwise
	 */
	public function publicOnCreateItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When using the item
	 * Show a form and validate the form
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	string|bool				string if there is a form to be used, true if successfully used, false otherwise
	 */
	public function publicUseItem(array &$invItem)
	{
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_items' ) );
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_usercp' ), 'core' );
		
		if(!is_dir($this->galleryDirectory))
		{
			$this->registry->getClass('output')->showError('gallery_avatar_dir_error');
		}
		if(!is_writeable($this->galleryDirectory))
		{
			$this->registry->getClass('output')->showError('gallery_avatar_dir_error');
		}
		
		if($this->request['request_method'] == 'post')
		{
			$exts = array();
			foreach(explode( ",", $this->ipsclass->vars['avatar_ext'] ) as $ext)
			{
				$exts[] = trim($ext);
			}
			
			require_once( IPS_KERNEL_PATH.'classUpload.php' );
			$upload = new classUpload();
			
			$upload->out_file_dir      = $this->galleryDirectory;
			$upload->max_file_size     = ($this->settings['avup_size_max'] * 1024) * 8;  // Allow xtra for compression
			$upload->upload_form_field = "upload_avatar";
			
			if ( is_array( $this->cache->getCache('attachtypes') ) and count( $this->cache->getCache('attachtypes') ) )
			{
				foreach( $this->cache->getCache('attachtypes') as $data )
				{
					if ( $data['atype_photo'] )
					{
						if( $data['atype_extension'] == 'swf' AND $this->settings['disable_flash'] )
						{
							continue;
						}
						
						$upload->allowed_file_ext[] = $data['atype_extension'];
					}
				}
			}
			
			$upload->process();
			
			if($upload->error_no)
			{
				$this->registry->getClass('output')->showError('upload_failed');
			}
			
			$this->deleteItems($invItem);
			
			$this->writeLog($invItem, "Uploaded gallery avatar, ");
			
			$this->registry->getClass('output')->redirectScreen( $this->lang->words['gallery_avatar_uploaded'], $this->settings['base_url_with_app'] . "module=inventory" );
		}
		
		$html = <<<EOF
<form id='galleryAvatarForm' enctype="multipart/form-data" method="post" action="{$this->settings['base_url_with_app']}module=inventory&amp;section=item&amp;item={$invItem['i_id']}">
	<div id='galleryAvatar'class='block_wrap'>
		<h3 id='galleryAvatarHeader' class='maintitle'>{$this->item['item_name']}</h3>
		<fieldset class='row1'>
			<h3>{$this->lang->words['avatar_url_title']}</h3>
			<p class='message unspecific'>
EOF;
		list( $this->settings['maxWidth'], $this->settings['maxHeight'] ) = explode( "x", strtolower($this->settings['avatar_dims'] ) );
		$html .= sprintf( $this->lang->words['av_text_url'], $this->settings['maxWidth'], $this->settings['maxHeight'] );
		$html .= <<<EOF
					<br />

EOF;
		$html .= sprintf( $this->lang->words['av_text_upload'], $this->settings['avup_size_max'] );
		$this->lang->words['av_allowed_files'] = sprintf($this->lang->words['av_allowed_files'], implode (' .', explode( "|", $this->settings['avatar_ext'] ) ) );
		$html .= <<<EOF
				<br />{$this->lang->words['av_allowed_files']}
			</p>
			<ul>
				<li class='field'>
					<label for='upload_avatar'>{$this->lang->words['av_upload']}</label>
					<input type="file" size="30" name="upload_avatar" id="upload_avatar" />
				</li>
			</ul>
		</fieldset>
		<fieldset class="submit">
			<input class="input_submit" type="submit" value="{$this->lang->words['upload_avatar']}" name="submit" />
		</fieldset>
	</div>
</form>
EOF;
		return $html;
	}
	
	/**
	 * Any AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function publicAjax(array &$invItem)
	{
		return;
	}
	
	/**
	 * When the item is being sold back
	 *
	 * @access	public
	 * @param	array	&$invItem	The inventory item information
	 * @return	bool				true if successful, false otherwise
	 */
	public function publicOnResoldItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When the item is being restocked
	 *
	 * @access	public
	 * @param	array	&$item	The inventory item information
	 * @return	void
	 */
	public function onItemRestock(array &$item)
	{
		return;
	}
}
